/* 
 * File:   main.cpp
 * Author: mcki4229
 *
 * Debugging exercise for CS 120
 * 
 * Created on March 30, 2012, 10:05 AM
 */

#include <cstdlib>
#include <iostream>
using namespace std;

/* Create a function with reference variables
 * Create a function with local variables
 * see how variables are set in debugger view
 * See how objects are represented in debugger view
 * 
 * Make a couple runtime errors for them to find ~index out of bounds, null pointer, etc.
 * 
 *
 */

//Function Prototypes
//Function that attempt to add b to a
void add(int a, int b); //by value

//Function that attempts to reverse a character array
void reverse(char in[]);

//function that attempts to divide two numbers
double divide(int dividend, int divisor);

void callStackDemo(int depth);

int main(){
    
        int x, y;
        x = 3;
        y = 5;
       
        //Example 1: add() does not work correctly. add(a, b) is supposed to store (a+b) into a
        add(x, y);
        cout << "Sum: " << x << endl; //The correct sum should be 8, not 3
        //What is the problem with this?
        //How can you fix it?

        //Example 2: Show call stack in debugger
        const int DEPTH = 5;
        callStackDemo(DEPTH);

        /*
         * The code below is buggy
         * Your job: examine it with the debugger and correct it
         */
        
        //Problem 1: Printing the reverse of a character array
        char test[11] = "Test Chars";
        reverse(test); //does not display the correct output: srahC tseT
        
        //Problem 2: Division
        double w, z, t;
        w = 4;
        z = 0.7;
        
        t = divide(w,z); // t = w/z
        cout << w << " divided by " << z << " = " << t << endl;
        
    return 0;
}

void add(int a, int b){
    a = a + b;
    cout << "Correct sum: " << a << endl;
}

void reverse(char in[]){
    //Print the reverse of a character array.
    //You of course can debug this without the help of the debugger, but examine why this isn't working with the debugger
    for(int i = 11; i > 0; i--){
        cout << in[i];
    }
    cout << endl;
}

double divide(int dividend, int divisor){
    //Divide a by b and return the result
    //You of course can debug this without the help of the debugger, but examine why this isn't working with the debugger
    double quotient = dividend/divisor;
    return quotient;
}

void callStackDemo(int depth){
    //Example recursive function to show call stack of debugger
    if(depth == 0){
        return;
    }
    callStackDemo(depth - 1);
}